<template>
  <div class="dialog-container">
    <el-dialog
      :title="dialogType==='add'?'添加资源':dialogType === 'detail'?'资源详情':'修改资源'"
      :visible="dialogVisible"
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
        :disabled="dialogType === 'detail'"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别" prop="resourceType">
              <el-select
                v-model="dataForm.resourceType"
                class="ipt-item w-220"
                placeholder="请选择类别"
              >
                <el-option
                  v-for="(item,index) in resourceTypes"
                  :key="index+'task_type'"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="resourceName">
              <el-input v-model="dataForm.resourceName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24" label="文件" prop="material">
            <el-form-item label="文件" prop="material">
              <UploadFile
                ref="uploadFile"
                :files="dataForm.enclosureUrl"
                :dialog-type="dialogType"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleSave">保存</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add } from '@/api/resources/resourceCenter'
import { getDictLabel } from '@/utils'
import { getDict } from '@/utils/dict'
import UploadFile from '@/components/UploadFile'

export default {
  name: 'AddResources',
  components: { UploadFile },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'detail'
    },
    obj: {
      type: Object,
      default: () => {
      }
    }
  },
  data() {
    return {
      saveLoading: false,
      dataForm: {
        resourceType: '',
        resourceName: null,
        enclosureUrl: null,
        remark: null
      },
      resourceTypes: [],
      rules: {
        resourceType: [{ required: true, message: '请选择资源类型', trigger: 'change' }],
        resourceName: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.resourceTypes = getDict('e028')
  },
  methods: {
    getDictLabel,
    initData() {
      this.$nextTick(() => {
        this.clearData()
      })
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const files = this.$refs.uploadFile.getFiles()
          if (files) {
            this.dataForm.enclosureUrl = JSON.stringify(files)
          }
          if (files === null || files.length === 0) {
            this.$message.error('未选择文件')
            return
          }
          this.saveLoading = true
          add(this.dataForm).then(() => {
            this.$message.success('成功')
            this.closeDialog()
            this.$emit('flushPage')
            this.clearData()
            this.saveLoading = false
          }).catch(() => {
            this.saveLoading = false
          })
        }
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.clearData()
    },
    clearData() {
      this.dataForm = {
        resourceType: '',
        resourceName: null,
        enclosureUrl: null,
        remark: null
      }
      this.$refs.uploadFile.initFile()
      this.$refs['form'].clearValidate()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
